package com.tomcat.mapper;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * desc:
 *
 * @author tengfei wang
 * @version 1.0
 * @date Created in 27/9/2020
 */
public class Mapper {

    public List<MappedHost> hosts = new ArrayList<>();

    protected static final class MappedHost extends MapElement<Host> {
        public volatile ContextList contextList;
        private final MappedHost realHost;

        public MappedHost(String name, Host host) {
            super(name, host);
            realHost = this;
            contextList = new ContextList();
        }
    }

    protected static final class ContextList {

        public final List<MappedContext> contexts;

        public ContextList() {
            this(new ArrayList<>());
        }

        public ContextList(List<MappedContext> contexts) {
            this.contexts = contexts;
        }
    }


    protected static final class MappedContext extends MapElement<Context> {
        public List<MappedWrapper> wrapper = null;

        public MappedContext(String path, Context context) {
            super(path, context);
            this.wrapper = new ArrayList<>();
        }

    }

    protected static class MappedWrapper extends MapElement<Wrapper> {
        public MappedWrapper(String name, Wrapper wrapper) {
            super(name, wrapper);
        }
    }

    protected abstract static class MapElement<T> {
        public final String name;
        public final T object;

        public MapElement(String name, T object) {
            this.name = name;
            this.object = object;
        }
    }

    public void addMappedHost(Host host) {
        MappedHost mappedHost = new MappedHost(host.getHostName(), host);
        hosts.add(mappedHost);
    }

    public void addContext(String host, Context context) {
        List<MappedHost> collect = hosts.stream().filter(w -> w.name.equalsIgnoreCase(host)).collect(toList());
        if (collect.size() > 0) {
            collect.get(0).contextList.contexts.add(new MappedContext(context.getContentName(), context));
        }
    }

    public void addWrapper(String host, String contextName, Wrapper wrapper) {
        List<MappedHost> collect = hosts.stream().filter(w -> w.name.equalsIgnoreCase(host)).collect(toList());
        if (collect.size() > 0) {
            List<MappedContext> contexts = collect.get(0).contextList.contexts;
            List<MappedContext> contextList = contexts.stream().filter(w -> w.name.equalsIgnoreCase(contextName)).collect(toList());
            if (contextList.size() > 0) {
                contextList.get(0).wrapper.add(new MappedWrapper(wrapper.getName(), wrapper));
            }
        }
    }

    /**
     * 封装mapperData
     * @param hostName host name
     * @param contextName context name
     * @param wrapperName servlet name
     * @return MapperData
     */
    public MapperData getMapperData(String hostName, String contextName, String wrapperName) {
        MapperData mapperData = new MapperData();
        List<MappedHost> collect = hosts.stream().filter(w -> w.name.equalsIgnoreCase(hostName)).collect(toList());
        if (collect.size() > 0) {
            mapperData.setHost(collect.get(0).object);
            List<MappedContext> contexts = collect.get(0).contextList.contexts;
            List<MappedContext> contextList = contexts.stream().filter(w -> w.name.equalsIgnoreCase(contextName)).collect(toList());
            if (contextList.size() > 0) {
                mapperData.setContexts(contextList.get(0).object);
                List<MappedWrapper> wrappers = contextList.get(0).wrapper.stream().filter(w -> w.name.equalsIgnoreCase(wrapperName)).collect(toList());
                if (wrappers.size() > 0) {
                    mapperData.setWrapper(wrappers.get(0).object);
                }
            }
        }
        return mapperData;
    }


}
